﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Common;
using System.Collections;
using StoragePeriod.Web.Common;
using StoragePeriod.Data;
using System.Text;

namespace StoragePeriod.Web.SysManage
{
    public partial class PrivilegeManage : SecurityPage
    {
        #region 继承
        public override string PrivCode()
        {
            return "54b9b6b09bdd5b05";
        }
        #endregion

        #region 函数

        private void DropBandingSearch()
        {
            List<OperationTeam> OperationList = OperationTeam.Where("ParentId=@0", 0).OrderByDescending(p => p.id).ToList();
            this.Drop_OperationSearch.DataSource = OperationList;
            this.Drop_OperationSearch.DataValueField = "id";
            this.Drop_OperationSearch.DataTextField = "OTName";
            this.Drop_OperationSearch.DataBind();
            this.Drop_OperationSearch.Items.Insert(0, new ListItem("全部", "-1"));
            this.Drop_OperationSearch_SelectedIndexChanged(null, null);
        }

        private void DropRoleBanding()
        {
            List<Role> RoleList = Role.All().ToList();
            this.Drop_RoleList.DataSource = RoleList;
            this.Drop_RoleList.DataValueField = "RoleCode";
            this.Drop_RoleList.DataTextField = "RoleName";
            this.Drop_RoleList.DataBind();
        }

        private void DataBing()
        {
            using (StoragePeriodDataContext data = new StoragePeriodDataContext())
            {
                var q = from f in data.SYS_Users
                        join A in data.OperationTeam on f.UserType equals A.id
                        select new
                        { 
                            f.id,
                            f.UserCode,
                            f.UserPWD,
                            f.UserName,
                            f.RoleCode,
                            f.RoleName,
                            f.UserType,
                            f.AuthType,
                            f.UserStatus,
                            f.EMail,
                            f.Hometown,
                            f.Political,
                            f.Sex,
                            f.EducatedLevel,
                            f.QQ,
                            f.Address,
                            f.BirthDate,
                            f.Specialty,
                            f.Unit,
                            f.IDNumber,
                            f.JobTitle,
                            f.MobileTelNo,
                            f.Descriptions,
                            f.CreatedTime,
                            ParentTemp=A.ParentId
                        };

                q = q.Where(p => p.UserName.Contains(this.txt_searchName.Text.Trim()));

                if (this.Drop_TeamSearch.SelectedValue != "-1" && this.Drop_TeamSearch.SelectedValue != "")
                {
                    q = q.Where(p => p.UserType == int.Parse(this.Drop_TeamSearch.SelectedValue));
                }
                else
                {
                    if (this.Drop_OperationSearch.SelectedValue != "" && this.Drop_OperationSearch.SelectedValue != "-1")
                    {
                        q = q.Where(p => p.ParentTemp == int.Parse(this.Drop_OperationSearch.SelectedValue));
                    }
                }

                this.RepDataList.DataSource = q.ToList();
                this.RepDataList.DataBind();
            }
        }

        int i = 0;
        public string GetXH()
        {
            i += 1;
            return i.ToString();
        }

        public string GetTeamName(string TeamId)
        {
            return OperationTeam.Where("id=@0", int.Parse(TeamId)).First().OTName;
        }

        #endregion

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropBandingSearch();
                DropRoleBanding();
                DataBing();
            }
        }

        protected void RepDataList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int RawTypeId = Convert.ToInt32(e.CommandArgument);
                SYS_Users md = SYS_Users.Where("id=@0", RawTypeId).First();
                this.HidTypeID.Value = RawTypeId.ToString();

                StringBuilder sb = new StringBuilder();
                sb.Append("'" + md.UserCode + "',");
                sb.Append("'" + md.RoleCode + "',");
                sb.Append("'" + md.UserPWD + "'");
                UtilityUI.Load(this.upPanelMain, "LoadData(" + sb.ToString() + ");ShowDlg(1)");
            }
        }

        protected void btPost_Click(object sender, EventArgs e)
        {
            int uid = Convert.ToInt32(this.HidTypeID.Value);

            if (SYS_Users.Where("id!=@0 and UserCode=@1", uid, this.txt_UserCode.Text.Trim()).Count() > 0)
            {
                UtilityUI.Load(this.upPanelMain, "alertstr('存在相同用户名！')");
                return;
            }

            SYS_Users md = SYS_Users.Where("id=@0", uid).First();
            md.UserCode = this.txt_UserCode.Text.Trim();
            if (md.UserPWD != this.txt_UserPWD.Text.Trim())
            {
                md.UserPWD = Utility.GetMD5PassWord(this.txt_UserPWD.Text.Trim(), "");
            }
            md.RoleCode = this.Drop_RoleList.SelectedValue;
            md.RoleName = this.Drop_RoleList.SelectedItem.Text;
            md.CreatedTime = DateTime.Now;
            SYS_Users.Update(md, "id=@0", md.id);

            RoleUsers md_RU = new RoleUsers();
            md_RU.UserCode = md.UserCode;
            md_RU.RoleCode = md.RoleCode;
            md_RU.UserName = md.UserName;

            RoleUsers md_RoleU = RoleUsers.Where("UserCode=@0", md.UserCode).FirstOrDefault();
            if (md_RoleU != null)
            {
                RoleUsers.Delete("UserCode=@0", md.UserCode);
            }
            RoleUsers.Insert(md_RU);

            DataBing();
            UtilityUI.Load(this.upPanelMain, "$('#dialog-form').dialog('close');;alertstr('修改成功！')");
        }

        protected void Drop_OperationSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.Drop_OperationSearch.SelectedItem != null)
            {
                int ParentId = Convert.ToInt32(this.Drop_OperationSearch.SelectedValue);
                List<OperationTeam> TeamList;
                if (ParentId == -1)
                {
                    TeamList = new List<OperationTeam>();
                }
                else
                {
                    TeamList = OperationTeam.Where("ParentId=@0", ParentId).ToList();
                }
                TeamList.Insert(0, new OperationTeam() { id = -1, OTName = "全部" });
                this.Drop_TeamSearch.DataSource = TeamList;
                this.Drop_TeamSearch.DataValueField = "id";
                this.Drop_TeamSearch.DataTextField = "OTName";
                this.Drop_TeamSearch.DataBind();
            }
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            DataBing();
        }

        #endregion
    }
}